import { request } from '@/api/request';
import { BASEAUTHTOKENFROMSBT, BASETOKENFROMSBT } from '@/common';
import { store } from '@/redux/store';

export type AssociationTreeDataDTO = {
  entId?: string;
  /** 查询类型 - 1: 备案人员; 2: 备案车辆 */
  type: number;
  /** 是否需要统计数量 - 默认 true */
  count: boolean;
  /**
   * NOTE: 需要区分sbt项目 还是安全教育项目
   *
   * 是否为安全教育平台 - 0: 否; 1: 是
   */
  isSafe?: number;
};

export type AssociationTreeDataVO = {
  entId: string;
  name: string;
  icon?: string;
  count: number;
};

export type PersonDataDTO = {
  pageIndex: number;
  pageSize: number;
  entId?: string;
  /** 人员职位 */
  positionLabelCode?: string;
  keyWords?: string;
};

export type PersonDataVO = {
  /** 头像 */
  icon?: string;
  /** 姓名 */
  name: string;
  /** 人员ID */
  personId: string;
  /** 电话 */
  phone?: string;
};

export type PositionDataDTO = {
  entId: string;
};

export type PositionDataVO = {
  /** 人员类型code  */
  code: string;
  /** 名称 */
  name: string;
  /** 备案人数 */
  personNum: number;
};

/**
 * 备案企业
 *
 * @param params
 */
export async function getAssociationTreeData(params: AssociationTreeDataDTO) {
  const { data } = await request<{ list: AssociationTreeDataVO[]; total: number }>({
    method: 'get',
    url: '/code/api/association/listByTree',
    params: { ...params, isSafe: 1 },
    noAutoShowErrorMessage: true,
    headers: {
      ClientType: 'WebSafe',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
  });

  return {
    list: data.list,
    total: data.total,
  };
}

/**
 * 备案人员
 *
 * @param params
 */
export async function getPersonListByEntId(params: PersonDataDTO) {
  const { data } = await request<{ list: PersonDataVO[]; total: number }>({
    method: 'get',
    url: '/code/api/association/person/list',
    params,
    noAutoShowErrorMessage: true,
    headers: {
      ClientType: 'WebSafe',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
  });

  return {
    list: data.list,
    total: data.total,
  };
}

/**
 * 人员类型
 *
 * @param params
 */
export async function getPositionListByEntId(params: PositionDataDTO) {
  const { data } = await request<PositionDataVO[]>({
    method: 'get',
    url: '/code/api/association/person/getPositionList',
    params,
    noAutoShowErrorMessage: true,
    headers: {
      ClientType: 'WebSafe',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
  });

  return {
    list: data,
    total: data && data?.length,
  };
}

/**
 * 查询去重后的人数
 *
 * @param params
 */
export async function gerPersonNum(params: {
  entId: string;
  publishObject: { id: string; name: string; phone?: string; type: number }[];
}) {
  const { data } = await request<number>({
    method: 'post',
    url: '/safe/api/safePlanTask/gerPersonNum',
    data: params,
    noAutoShowErrorMessage: true,
  });

  return data;
}
